/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameheartv2;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;

/**
 *
 * @author GaoXteen
 */
public class GameStateRoom extends GameState {

    public Image image;
    BufferedImage img;
    public static JTextField tfHostIP;

    public static JTable hostTable;
    public static JScrollPane scrollPane;
    static Vector colums= new Vector( );
    public static JButton btJoin;
    public static JButton btCreate;
    public static JButton btRefresh;
    static public Server server;
    static public UDPServer udpServer;
    static public UDPClient udbClient;
    static public Vector<String> Server;
    static Vector data;
    static public JLabel faceLabel;
    static public JTextField nameField;
     public static JPanel imagePane  = new JPanel();
     static public File file;
     static public JTextField name;

    public GameStateRoom(GameStateFrame _mainGame) {

        this.mainGame = _mainGame;
        this.width = 1024;
        this.height = 738;
        String bgTField = System.getProperty("user.dir")+"\\src\\Resources\\bg_TextBox.jpg";
        try {
            String fileName =  System.getProperty("user.dir")+"\\src\\Resources\\bg_main1.jpg";
            img = ImageIO.read(new File(fileName));
        }catch (IOException e) {}
        this.GAME_STATE = GameDef.STATE_ROOM;


    }

    @Override
    public void MessageEnter(int message) {
        mainGame.setSize(width, height);
        Font font1 = new Font("Arial", Font.BOLD, 15);
        tfHostIP = new JTextField();
        tfHostIP.setBounds(279, 539, 338, 26);
        Color co = new Color(255,235,184);
        tfHostIP.setBackground(co);
        tfHostIP.setFont(font1);
        tfHostIP.setEditable(false);
        mainGame.getLayeredPane().add(tfHostIP, new Integer(-4));
        String ip="";
        try {
            InetAddress ipAddress =InetAddress.getLocalHost();
            ip = ipAddress.toString();
        }
        catch(Exception ex){}
        tfHostIP.setText(ip);

        colums.add("STT");
        colums.add("IP Host");
       // colums.add("Post");
         data = new Vector();    
        
        MyTableModel tableModel = new MyTableModel(colums,data);
        hostTable = new JTable(tableModel){
              public boolean isCellEditable(int rowIndex, int colIndex) {
              return false; //Disallow the editing of any cell
              }
        };
        scrollPane = new JScrollPane(hostTable);
        hostTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        hostTable.setFillsViewportHeight(true);
        hostTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        hostTable.getColumnModel().getColumn(0).setPreferredWidth(75);
        hostTable.getColumnModel().getColumn(1).setPreferredWidth(520);    
        Color co2 = new Color(178,193,76);
        hostTable.setBackground(co2);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.setBounds(100, 260, 595, 220);
        JTableHeader anHeader = hostTable.getTableHeader();
        anHeader.setForeground(Color.WHITE);
        Color co1 = new Color(105,150,49);
        anHeader.setBackground(co1);
        anHeader.setFont(font1);
        mainGame.getLayeredPane().add(scrollPane, new Integer(-4));

        String path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnThamGia.jpg";
        ImageIcon bgCard = new ImageIcon(path);
        btJoin = new JButton(bgCard);
        btJoin.setBounds(300, 600, 90, 32);
        btJoin.setBorder(BorderFactory.createEmptyBorder());
        btJoin.addMouseListener(new ThamGiaMouseListener());
        mainGame.getLayeredPane().add(btJoin, new Integer(-4));

        path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnTaoHost.jpg";
        bgCard = new ImageIcon(path);
        btCreate = new JButton(bgCard);
        btCreate.setBounds(400, 600, 90, 32);
        btCreate.setBorder(BorderFactory.createEmptyBorder());
        btCreate.addMouseListener(new TaoHostMouseListener());
        mainGame.getLayeredPane().add(btCreate, new Integer(-4));

        path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnCapNhat.jpg";
        bgCard = new ImageIcon(path);
        btRefresh = new JButton(bgCard);
        btRefresh.setBounds(500, 600, 90, 32);
        btRefresh.setBorder(BorderFactory.createEmptyBorder());
        btRefresh.addMouseListener(new CapNhatMouseListener());
        mainGame.getLayeredPane().add(btRefresh, new Integer(-4));

        name = new JTextField();
        name.setBounds(801, 200, 190, 20);
        name.setBackground(co);
        name.setText(mainGame.player.name);
        name.setBorder(BorderFactory.createEmptyBorder());
        mainGame.getLayeredPane().add(name, new Integer(-4));
        String face = GameDef.FacePath+mainGame.player.imagePath;
        file = new File(face);
        ImageIcon icon = new ImageIcon();
        try
        {
            BufferedImage myPicture = ImageIO.read(file);
             icon = new ImageIcon(myPicture);
        }
        catch(Exception ex){}

        faceLabel = new JLabel();       
        faceLabel.setBackground(co1);
        imagePane  = new JPanel();
        imagePane.add(faceLabel);        
        faceLabel.setIcon(icon);
        imagePane.setBounds(801, 139, 40, 40);
        co1 = new Color(44,88,0);
        imagePane.setBackground(co1);
        mainGame.getLayeredPane().add(imagePane, new Integer(4));
        faceLabel.setIcon(icon);

        faceLabel.addMouseListener(new FaceMouseListener());



        GameStateRoom.udbClient = new UDPClient();
        GameStateRoom.udbClient.start();
    }


    public static void ChangeState(int state)
    {
        GameStateRoom.btCreate.setVisible(false);
        GameStateRoom.btRefresh.setVisible(false);
        GameStateRoom.btJoin.setVisible(false);
        GameStateRoom.scrollPane.setVisible(false);
        GameStateRoom.tfHostIP.setVisible(false);
        GameStateRoom.imagePane.setVisible(false);
        GameStateFrame.SwitchState(GameDef.STATE_TABLECARD);
        GameStateRoom.udbClient.start();
    }

    @Override
    public void MessageUpdate(int message) {

    }

   

    @Override
    public void MessagePaint(int message) {
       this.gameRender();
       mainGame.update(mainGame.getGraphics());

    }

     public void gameRender()
        {
            if (mainGame.dbImage == null){
                    mainGame.dbImage  = mainGame.createImage(width, height);
              if (mainGame.dbImage  == null) {
                System.out.println("dbImage is null");
                return;
              }
              else
                gp2d = mainGame.dbImage.getGraphics();
            }
            gp2d.drawImage(this.img, 0, 0, null);
        }

      class CapNhatMouseListener implements MouseListener{

          public void mouseClicked(MouseEvent e)
          {

                GameStateRoom.data.clear();
                if(GameStateRoom.udbClient!=null)
                {
                    GameStateRoom.udbClient.stop();
                    GameStateRoom.udbClient.start();
                }

          }

          public void mouseEntered(MouseEvent e)
            {
              String path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnCapNhatHover.jpg";
              ImageIcon bgCard = new ImageIcon(path);
                 ((JButton)e.getComponent()).setIcon(bgCard);
            }

          public void mouseExited(MouseEvent e)
            {
               String path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnCapNhat.jpg";
              ImageIcon bgCard = new ImageIcon(path);
                 ((JButton)e.getComponent()).setIcon(bgCard);
            }

          public void mousePressed(MouseEvent e)
            {

            }

          public void mouseReleased(MouseEvent e)
            {

          }
      }

      class TaoHostMouseListener implements MouseListener{

          public void mouseClicked(MouseEvent e)
          {
              GameStateRoom.udpServer = new UDPServer();
              GameStateRoom.udpServer.start();

              GameStateRoom.server = new Server(8999);
              GameStateRoom.server.start();
              InetAddress ad;
              try
              {
              mainGame.ip=InetAddress.getLocalHost().getHostAddress().toString();
              }
              catch(Exception ex)
              {
              
              }
              mainGame.player.name = name.getText().trim();
              GameStateTableCard.client = new Client(mainGame.ip,8999);
              String message = "3 "+mainGame.player.name +" "+mainGame.player.imagePath;
              GameStateTableCard.client.send(message);
             //GameStateRoom.ChangeState(GameDef.STATE_TABLECARD);

          }

          public void mouseEntered(MouseEvent e)
            {
              String path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnTaoHostHover.jpg";
              ImageIcon bgCard = new ImageIcon(path);
                 ((JButton)e.getComponent()).setIcon(bgCard);
            }

          public void mouseExited(MouseEvent e)
            {
               String path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnTaoHost.jpg";
              ImageIcon bgCard = new ImageIcon(path);
                 ((JButton)e.getComponent()).setIcon(bgCard);
            }

          public void mousePressed(MouseEvent e)
            {

            }

          public void mouseReleased(MouseEvent e)
            {

          }
      }

      class ThamGiaMouseListener implements MouseListener{

          public void mouseClicked(MouseEvent e)
          {
                 String ip = "";
                 int indexRow = GameStateRoom.hostTable.getSelectedRow();
                 ip = (String)GameStateRoom.hostTable.getModel().getValueAt(indexRow, 1);
                 GameStateRoom.tfHostIP.setText(ip);
                 mainGame.ip=ip.substring(1);
                 GameStateTableCard.client = new Client(mainGame.ip,8999);
                 mainGame.player.name = name.getText().trim();
                    String message = "3 "+mainGame.player.name +" "+mainGame.player.imagePath;
                    GameStateTableCard.client.send(message);
                              
                 //GameStateRoom.ChangeState(GameDef.STATE_TABLECARD);
          }

          public void mouseEntered(MouseEvent e)
            {
              String path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnThamGiaHover.jpg";
              ImageIcon bgCard = new ImageIcon(path);
                 ((JButton)e.getComponent()).setIcon(bgCard);
            }

          public void mouseExited(MouseEvent e)
            {
               String path =  System.getProperty("user.dir")+"\\src\\Resources\\bg_btnThamGia.jpg";
              ImageIcon bgCard = new ImageIcon(path);
                 ((JButton)e.getComponent()).setIcon(bgCard);
            }

          public void mousePressed(MouseEvent e)
            {

            }

          public void mouseReleased(MouseEvent e)
            {

          }
      }

      class FaceMouseListener implements MouseListener{

          public void mouseClicked(MouseEvent e)
          {
              String path =  System.getProperty("user.dir")+"\\src\\Resources\\Face";
                 JFileChooser fc = new JFileChooser(path);
               int returnVal = fc.showOpenDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    file = fc.getSelectedFile();
                    //This is where a real application would open the file.
                    ImageIcon icon = new ImageIcon();
                    try
                    {
                        BufferedImage myPicture = ImageIO.read(file);
                         icon = new ImageIcon(myPicture);
                    }
                    catch(Exception ex){}
                    GameStateRoom.faceLabel.setIcon(icon);
                    mainGame.player.imagePath = file.getName();
                }
          }

          public void mouseEntered(MouseEvent e)
            {

            }

          public void mouseExited(MouseEvent e)
            {

            }

          public void mousePressed(MouseEvent e)
            {

            }

          public void mouseReleased(MouseEvent e)
            {

          }
      }

}